<template>
  <div class="menu-wrapper">
    <!--    <company-dialog ref="officeDialog" @confirm="dialogConfirm" />-->
    <!--    <office-dialog ref="officeDialog" @confirm="officeConfirm"/>-->
    <el-form ref="form" :model="form" :rules="rules" label-width="160px">
      <div
        style="width: 100%;display: flex;align-items: flex-end;border-bottom: 1px #eee solid;margin-bottom: 20px"
      >
        <span style="font-size: 18px;color:#409EFF;margin: 10px 0;">修改账户信息</span>
      </div>
      <el-form-item label="公司名称:" prop="name">
        <el-col :span="6">
          <el-input v-model="form.name" clearable/>
        </el-col>
      </el-form-item>
      <!--<el-form-item label="公司 ID:" prop="id">-->
        <!--<el-col :span="6">-->
          <!--<span disabled>{{form.id}}</span>-->
        <!--</el-col>-->
      <!--</el-form-item>-->
      <!--<el-form-item-->
        <!--label="状态:"-->
        <!--prop="type">-->
        <!--<el-col :span="6">-->
          <!--<el-tag :type="form.status == 1?'success':'info'">-->
            <!--<span>{{ form.status | watchStatus }}</span>-->
          <!--</el-tag>-->
        <!--</el-col>-->
      <!--</el-form-item>-->
      <el-form-item label="logo:" prop="logo">
        <single-upload :singleUploadTitle="singleUploadTitle" v-model="form.logo"
                       style="display: inline-block; width: 500px" v-if="imgShow==true"></single-upload>
        <img :src="form.logo" style="width:70px;height:70px" alt="" v-if="form.logo!=null&&imgShow==false">
        <span v-if="form.logo==null&&imgShow==false">您没有上传logo</span>
      </el-form-item>
      <el-form-item label="公司类型:" prop="type">
        <el-col :span="6">
          <el-select style="width: 100%" v-model="form.type">
            <el-option v-for="item in typelist" :key="item.id" :label="item.name" :value="item.id" :disabled="item.id == 1">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="联系人:" prop="linkman">
        <el-col :span="6">
          <el-input v-model="form.linkman" clearable/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系电话:" prop="phone">
        <el-col :span="6">
          <el-input v-model="form.phone" clearable/>
        </el-col>
      </el-form-item>
      <el-form-item label="详细地址:" prop="address">
        <el-col :span="6">
          <el-input v-model="form.address" clearable/>
        </el-col>
      </el-form-item>
      <div style="width: 100%;display: flex;align-items: flex-end;border-bottom: 1px #eee solid;margin-bottom: 20px">
        <span style="font-size: 18px;color:#409EFF;margin: 10px 0;">企业资料</span>
      </div>

      <el-form-item label="身份证号:" prop="legalPersonCode">
        <el-col :span="6">
          <el-input v-model="form.metaInfo.legalPersonCode"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="开户行:" prop="bankname">
        <el-col :span="6">
          <el-input v-model="form.metaInfo.bankname"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="开户行账号:" prop="banknum">
        <el-col :span="6">
          <el-input v-model="form.metaInfo.banknum"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="统一社会信用代码:" prop="meta">
        <el-col :span="6">
          <el-input v-model="form.metaInfo.licenseCode"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="身份证正面:" prop="legalPerson1">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.legalPerson1"
                      style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.legalPerson1" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.legalPerson1!=null&&imgShow==false">
        <span v-if="form.metaInfo.legalPerson1==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="身份证反面:" prop="legalPerson2">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.legalPerson2"
                      style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.legalPerson2" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.legalPerson2!=null&&imgShow==false">
        <span v-if="form.metaInfo.legalPerson2==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="开户许可证:" prop="accountLicense">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.accountLicense"
                      style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.accountLicense" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.accountLicense!=null&&imgShow==false">
        <span v-if="form.metaInfo.accountLicense==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="营业执照:" prop="license">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.license"
                      style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.license" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.license!=null&&imgShow==false">
        <span v-if="form.metaInfo.license==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="法人代表人授权书:" prop="legalGrant">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.legalGrant"
                      style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.legalGrant" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.legalGrant!=null&&imgShow==false">
        <span v-if="form.metaInfo.legalGrant==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="危险化学品经营许可证:" prop="chemical">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.chemical"
                      style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.chemical" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.chemical!=null&&imgShow==false">
        <span v-if="form.metaInfo.chemical==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="批发经营证书:" prop="wholesale">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.wholesale"
                      style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.wholesale" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.wholesale!=null&&imgShow==false">
        <span v-if="form.metaInfo.wholesale==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="其他证件" prop="other">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.other"
                      style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.other" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.other!=null&&imgShow==false">
        <span v-if="form.metaInfo.other==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :loading="clicked"
          v-permission="['company.edit']"
          @click="updateCompany('form')">修 改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style scoped>
.menu-wrapper {
  margin: 15px;
}
</style>
<script>
import {EventBus} from "@/api/eventBus";
import utils from "../../utils/utils.js";
// import Distpicker from "@/components/Distpicker/Distpicker";
import {update, find} from "@/api/company";
import md5 from "js-md5";
import QueryGroup from "@/components/Query/queryGroup";
import QueryCompany from "@/components/Query/queryCompany";
import singleUpload from "@/components/Upload/singleUpload";
import Security from "@/views/company/components/Security";
import officeDialog from '@/components/OfficeDialog'
import multiUpload from "@/components/Upload/multiUpload";
export default {
  name: "company-edit",
  filters: {
    watchType(val) {
      if (val == 1) {
        return '供应商'
      } else {
        return '采购方'
      }
    },
    watchStatus(val) {
      if (val == 1) {
        return '已认证'
      } else
      if (val == 2) {
        return '已驳回'
      } else {
        return '未认证'
      }
    },
  },
  components: {
    Security,
    QueryGroup,
    QueryCompany,
    officeDialog,
    singleUpload,
    multiUpload,
  },
  data() {
    return {
      activeTab: 'account',
      imgShow: true,
      singleUploadTitle: "100*100",
      pageNum: 1,
      pageSize: 20,
      userId: '',
      userName: '',
      clicked: false, // 按钮防双击
      form: {
        id: '',
        name: "",        //公司名称
        logo: "",         //公司Logo
        address: "",	    //详情地址
        linkman: "",	    //联系人
        phone: "",        //联系电话
        companyId: '',    //所属账户
        groupId: '',     //所属集团
        userId: '',       //管理员
        meta: '',         //审核资料
        metaInfo :{
          chemical:"",
          license :"",
          licenseCode:'',
          other:"",
          wholesale:"",
          accountLicense: "",
          bankname: "",
          banknum: "",
          legalGrant: "",
          legalPerson1: "",
          legalPerson2: "",
          legalPersonCode: "",
        },
        msg: "",          //审核说明
        status: 0,        //状态(0.未认证,1.已认证)
        type: null,       //客户类型(1.供应商,2.采购方)
        // createDate :"",   //创建日期
        // modifyDate :"",   //修改日期
      },
      rules: {
        name: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
          { min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur" }
        ],
        linkman: [
          { required: true, message: "请输入联系人", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "请输入联系电话", trigger: "blur" }
        ],

        groupId:[
          { required: true, message: "请选择所属集团", trigger: "blur"}
        ],
        legalPerson1: [
          {required: true, message: "请上传身份证正面", trigger: "blur"}
        ],
        legalPerson2: [
          {required: true, message: "请上传身份证反面", trigger: "blur"}
        ],
        legalPersonCode: [
          {required: true, message: "请输入身份证号码", trigger: "blur"}
        ],
      },
      typelist:[
        {
          id :1,
          name:"经纪人(个人)"
        },{
          id :2,
            name:"法人企业"
        }
      ],
      query:{
        id:'',
        name:""
      }

    };
  },
  created() {
    var _this = this;
    _this.getData();
  },
  watch: { // 路由地址不变的情况下,如果id变化则重新获取数据
    $route(newVal, oldVal) {
      if (newVal.name == 'company-edit' && newVal.query.id != this.id) {
        this.getData()
      }
    },
  },
  methods: {
//    remoteMethod(query) {

      // this.loadCommuityData(query)
//      this.queryCompanyClick()
//      this.queryGroupClick()
//      this.selectType()

//    },

    queryCompanyClick(val) {
      console.log("选择的是",val)
      this.form.companyId = val.companyId;
      this.form.companyName = val.name;
      console.log("选择的账户是",this.form.companyName)
    },
    queryGroupClick(val) {
      console.log("选择的是",val)
      this.form.groupId = val.groupId
      this.form.groupName = val.name
      console.log("选择的集团是",this.form.groupName)
    },

    getData() {
      this.form.id = this.$route.query.id;
      var _this = this;
      find(_this.form.id)
        .then(response => {
         console.log("请求返回的数据" + JSON.stringify(response));
          if (response.type == "success") {
             _this.form = response.data.company;
             if(!utils.isNull(response.data.company.metaInfo)){
               var metaInfo = {
                 chemical:"",
                 license :"",
                 licenseCode:'',
                 other:"",
                 wholesale:"",
                 accountLicense: "",
                 bankname: "",
                 banknum: "",
                 legalGrant: "",
                 legalPerson1: "",
                 legalPerson2: "",
                 legalPersonCode: "",
               };
               _this.form.metaInfo = metaInfo
             }
          } else {
            _this.$message({
              message: response.content,
              offset: 120,
              center: true
            });

          }
        })
        .catch(error => {
        });
    },

    updateCompany(form) {
      const _this = this;
      console.log("form",this.$refs[form].model);
      // console.log("点击创建了", this.$refs[form],)
      this.$refs[form].validate(valid => {
        console.log("点击创建了", valid)
        if (valid) {
          // 校验表单
          var list = {
            id :_this.form.id ,
            name :_this.form.name ,
            logo : _this.form.logo ,
            status : _this.form.status ,
            groupId :_this.form.groupId ,
            companyId :_this.form.companyId ,
            userId : _this.form.userId ,
            phone :_this.form.phone ,
            linkman :_this.form.linkman ,
            address :_this.form.address ,
            meta :_this.form.meta ,
            msg :_this.form.msg ,
            type :_this.query.id ,
          }
          _this.clicked = true;
          update(list)
            .then(response => {
              _this.clicked = false;
              if (response.type == "success") {
                let data = response.data;
                console.log("修改完返回给列表的数据--" + JSON.stringify(data));
                EventBus.$emit("company-edit", data);
                _this.$store
                  .dispatch("tagsView/delView", _this.$route)
                  .then(({visitedViews}) => {
                    _this.$router.push({
                      path: "/company/list"
                    });
                  }); // 关闭当前tab回到列表
              } else {
                _this.$message({
                  message: response.content,
                  offset: 120,
                  center: true
                });
                console.log("content",response.content);

              }
            })
            .catch(error => {
              _this.clicked = false;
            });
        } else {
          return false;
        }
      });
    },
    //过滤成日期
    formatDate2Day(val) {
      let date = new Date(val);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      return y + "-" + MM + "-" + d;
    },

    //过滤到时间
    formatDate2Time(val) {
      let date = new Date(val);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let m = date.getMinutes();
      m = m < 10 ? "0" + m : m;
      let s = date.getSeconds();
      s = s < 10 ? "0" + s : s;
      return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
    },

    //过滤企业列表
//    formatList(data) {
//      data.createDate = this.formatDate2Day(data.createDate);
//      data.modifyDate = this.formatDate2Time(data.modifyDate);
//      data.rechangeDate = this.formatDate2Time(data.rechangeDate);
//      return data;
//    },
  }
};
</script>
